#include "../include/initMouse.h"
#include "../include/video.h"

/**static coor mouse_cursor = {0,0};
*/

extern outportb(int port, int byte);
extern inportb( int port);


void mouse_wait(unsigned char type)
{
  unsigned int _time_out=100000;
  if(type==0)
  {
    while(_time_out--) //Data
    {
      if((inportb(0x64) & 1)==1)
      {
        return;
      }
    }
    return;
  }
  else
  {
    while(_time_out--) //Signal
    {
      if((inportb(0x64) & 2)==0)
      {
        return;
      }
    }
    return;
  }
};

void mouse_write(unsigned char a_write)
{
	mouse_wait(1);
	outportb(0x64, 0xD4);
	mouse_wait(1);
	outportb(0x60, a_write);
};

unsigned char mouse_read(void)
{
	mouse_wait(0);
	return inportb(0x60);
};

void mouse_init(void){

	unsigned char status_byte;

	mouse_wait(1);
	outportb(0x64,0xA8);
	mouse_wait(1);
	outportb(0x64,0x20);
	
	mouse_wait(0);
	status_byte = (inportb(0x60) | 2);
	mouse_wait(1);
	outportb(0x64, 0x60);
	mouse_wait(1);
	outportb(0x60, status_byte);
	mouse_write(0xF6);
	mouse_read();
	mouse_write(0xF4);
	mouse_read();
};
